/*
  **所有方法返回的key都是base64编码
*/

package com.mixed;

import java.io.*; 
import java.security.*;  
import javax.crypto.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
//import org.apache.commons.codec.binary.Base64;使用上面的包
import javax.crypto.spec.*;

public class DES {
    
    public static String getKey() throws Exception{
        KeyGenerator kg=KeyGenerator.getInstance("DESede");
        kg.init(168); 
        SecretKey k=kg.generateKey();
        return (new BASE64Encoder()).encode(k.getEncoded());
    }

    public static void getKeyToFile(String path) throws Exception {
        String key = getKey();
        FileWriter fw = new FileWriter(path);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(key);
        bw.flush();
    }

    //key 是base64编码的字符串
    public static void encrypt(String plainpath, String key, String cipherpath) throws Exception{
        try{
            byte[] keyBytes = (new BASE64Decoder()).decodeBuffer(key);
            DESedeKeySpec dks = new DESedeKeySpec(keyBytes);//实例化DES密钥
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");//实例化密钥工厂
            SecretKey k = skf.generateSecret(dks);//生成密钥
            Cipher cipher = Cipher.getInstance("DESede");   
            cipher.init(Cipher.ENCRYPT_MODE, k); 
            InputStream is = new FileInputStream(plainpath);   
            OutputStream out = new FileOutputStream(cipherpath);   
            CipherInputStream cis = new CipherInputStream(is, cipher);   
            byte[] buffer = new byte[1024];   
            int r;   
            while ((r = cis.read(buffer)) > 0) {   
                out.write(buffer, 0, r);   
            }   
            cis.close();   
            is.close();   
            out.close();   
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void decrypt(String cipherpath, String key, String plainpath) throws Exception{
        try{
            OutputStream out = new FileOutputStream(plainpath);
            byte[] keyBytes = (new BASE64Decoder()).decodeBuffer(key);
            DESedeKeySpec dks = new DESedeKeySpec(keyBytes);//实例化DES密钥
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");//实例化密钥工厂
            SecretKey k = skf.generateSecret(dks);//生成密钥
            Cipher cipher = Cipher.getInstance("DESede");   
            cipher.init(Cipher.DECRYPT_MODE, k);   
            InputStream is = new FileInputStream(cipherpath);   
            CipherOutputStream cos = new CipherOutputStream(out, cipher);   
            byte[] buffer = new byte[1024];   
            int r;   
            while ((r = is.read(buffer)) >= 0) {   
                System.out.println();  
                cos.write(buffer, 0, r);   
            }   
            cos.close();   
            out.close();   
            is.close();   
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void DESEncrypt(String plainpath, String keypath, String cipherpath) throws Exception{
        String key = loadKeyByFile(keypath);
        encrypt(plainpath, key, cipherpath);
    }

    public static void DESDecrypt(String cipherpath, String keypath, String plainpath) throws Exception {
        String key = loadKeyByFile(keypath);
        decrypt(cipherpath, key, plainpath);
    }

    public static String loadKeyByFile(String path) throws Exception {
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            String readLine = null;
            StringBuilder sb = new StringBuilder();
            while ((readLine = br.readLine()) != null) {
                sb.append(readLine);
            }
            br.close();
            return sb.toString();
        } catch (Exception e) {
            throw new Exception("Load key failed");

        }
    }
}